#!/bin/bash

echo "Current time: $(date)"
echo "Starting container initialization..."

# 确保必要的目录存在并且权限正确
mkdir -p /var/log/nginx /var/lib/nginx /var/run/nginx
chown -R devel:devel /var/log/nginx /var/lib/nginx /var/run/nginx /workspace

echo "Starting nginx as root..."
nginx

if [ $? -ne 0 ]; then
    echo "Failed to start nginx. Exiting."
    exit 1
fi

echo "Nginx started successfully."

echo "Switching to devel user..."
su - devel << EOT
echo "Now running as devel user."
while true; do
    sleep 60
    if ! pgrep -x "nginx" > /dev/null; then
        echo "Nginx is not running. Exiting."
        exit 1
    fi
done
EOT
